﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using MobileWebsiteGenerator.Utility;
using System.Data;
using MobileWebsiteGenerator;
using MobileWebsiteGenerator.Entities;
using MobileWebsiteGenerator.Models;

namespace MobileWebsiteGenerator.Controllers
{
    public class PageController : HomeController
    {
        public Mobile_appContext context = new Mobile_appContext();
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Page/Create


        [HttpPost]
        public ActionResult Create(Page page)
        {
            if (ModelState.IsValid)
            {

                page.page_id = MvcApplication._MainCustomer.ContentPages != null && MvcApplication._MainCustomer.ContentPages.Count > 0 ? MvcApplication._MainCustomer.ContentPages.Count() + 1 : 1;
                var mypriority = page.Priority;
           
                    
                MvcApplication._MainCustomer.ContentPages.Add(page);

                MvcApplication._MainCustomer.ContentPages = MvcApplication._MainCustomer.ContentPages.OrderBy(p => p.Priority).ToList();


                MvcApplication.showMessage = true;
                MvcApplication.messageTxt = "Page Created Successfully";


                return RedirectToAction("Create", "Website");
            }

            return View(page);
        }

        //
        // GET: /Page/Edit/5

        public ActionResult Edit(int id)
        {
            Page page = MvcApplication._MainCustomer.ContentPages.Single(x => x.page_id == id);
            return View(page);
        }

        //
        // POST: /Page/Edit/5

        [HttpPost]
        public ActionResult Edit(Page page)
        {
            if (ModelState.IsValid)
            {

                var Prepage = MvcApplication._MainCustomer.ContentPages.Where(p => p.page_id == page.page_id).FirstOrDefault();
                page.page_id = Prepage.page_id;
                MvcApplication._MainCustomer.ContentPages.Remove(Prepage);
                MvcApplication._MainCustomer.ContentPages.Add(page);
                MvcApplication._MainCustomer.ContentPages = MvcApplication._MainCustomer.ContentPages.OrderBy(p => p.Priority).ToList();
                MvcApplication.showMessage = true;
                MvcApplication.messageTxt = "Page Updated Successfully";
                return View(page);
            }


            return View(page);
        }

        //
        // GET: /Page/Delete/5


        public string Delete(int id)
        {
            var Prepage = MvcApplication._MainCustomer.ContentPages.Where(p => p.page_id == id).FirstOrDefault();

            MvcApplication._MainCustomer.ContentPages.Remove(Prepage);
            MvcApplication._MainCustomer.ContentPages = MvcApplication._MainCustomer.ContentPages.OrderBy(p => p.Priority).ToList();


            return "done";
        }


        public string DeleteContactUs(int id)
        {
            var Prepage = MvcApplication._MainCustomer.ContentPages.Where(p => p.page_id == id).FirstOrDefault();
            MvcApplication._MainCustomer.Website.IsContactusPage = false;
            MvcApplication.pageId = 0;
            MvcApplication._MainCustomer.ContentPages.Remove(Prepage);
            MvcApplication._MainCustomer.ContentPages = MvcApplication._MainCustomer.ContentPages.OrderBy(p => p.Priority).ToList();

            return "done";
        }

        public string AddContactUS(int id)
        {
            var Prepage = context.Pages.Where(p => p.page_id == id).FirstOrDefault();
            MvcApplication._MainCustomer.Website.IsContactusPage = true;
            Prepage.page_id = MvcApplication._MainCustomer.ContentPages.Count() == 0 ? 1 : MvcApplication._MainCustomer.ContentPages.Count() + 1;
            MvcApplication._MainCustomer.ContentPages.Add(Prepage);
            MvcApplication._MainCustomer.ContentPages = MvcApplication._MainCustomer.ContentPages.OrderBy(p => p.Priority).ToList();
            MvcApplication.pageId = Prepage.page_id;
            return "done";
        }

    }
}
